package com.ec.page.service;

import java.io.File;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import com.ec.commodity.pojo.Brand;
import com.ec.commodity.pojo.Category;
import com.ec.commodity.pojo.Sku;
import com.ec.commodity.pojo.SpecGroup;
import com.ec.commodity.pojo.Spu;
import com.ec.commodity.pojo.SpuDetail;
import com.ec.common.enums.ExceptionEnum;
import com.ec.common.exception.ECException;
import com.ec.page.client.BrandClient;
import com.ec.page.client.CategoryClient;
import com.ec.page.client.GoodsClient;
import com.ec.page.client.SpecClient;

import lombok.extern.slf4j.Slf4j;

/**
 * 静态页面的Service
 * @author bystander
 * @date 2018/9/26
 */
@Slf4j
@Service
public class PageService {

	@Autowired
	private BrandClient brandClient;

	@Autowired
	private CategoryClient categoryClient;

	@Autowired
	private GoodsClient goodsClient;

	@Autowired
	private SpecClient specClient;

	@Autowired
	private TemplateEngine templateEngine;

	@Value("${ec.page.path}")
	private String dest;

	public Map<String, Object> loadModel(Long spuId) {
		Map<String, Object> model = new HashMap<>();
		Spu spu = goodsClient.querySpuBySpuId(spuId);

		// 上架未上架，则不应该查询到商品详情信息，抛出异常
		if (!spu.getSaleable()) {
			throw new ECException(ExceptionEnum.GOODS_NOT_SALEABLE);
		}

		SpuDetail detail = spu.getSpuDetail();
		List<Sku> skus = spu.getSkus();
		Brand brand = brandClient.queryById(spu.getBrandId());
		// 查询三级分类
		List<Category> categories = categoryClient
				.queryByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));

		List<SpecGroup> specs = specClient.querySpecsByCid(spu.getCid3());

		model.put("brand", brand);
		model.put("categories", categories);
		model.put("spu", spu);
		model.put("skus", skus);
		model.put("detail", detail);
		model.put("specs", specs);
		return model;
	}

	public void createHtml(Long spuId) {
		Context context = new Context();
		Map<String, Object> map = loadModel(spuId);
		context.setVariables(map);

		File file = new File(this.dest, spuId + ".html");
		// 如果页面存在，先删除，后进行创建静态页
		if (file.exists()) {
			file.delete();
		}
		try (PrintWriter writer = new PrintWriter(file, "utf-8")) {
			templateEngine.process("item", context, writer);
		} catch (Exception e) {
			log.error("【静态页服务】生成静态页面异常", e);
		}
	}

	public void deleteHtml(Long id) {
		File file = new File(this.dest + id + ".html");
		if (file.exists()) {
			boolean flag = file.delete();
			if (!flag) {
				log.error("删除静态页面失败");
			}
		}
	}
}
